package com.lemon.smartsafetynew.ui.adapter.monitor;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.entity.monitor.MonitorItemVo;

import java.util.List;

import butterknife.Bind;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2017/8/8.
 */

public class MonitorItemAdapter extends LemonRcvAdapter<MonitorItemVo> {

    private RecyclerTClickListener<MonitorItemVo> mListener;

    public MonitorItemAdapter(@Nullable List<MonitorItemVo> data,
                              RecyclerTClickListener<MonitorItemVo> mListener) {
        super(data);
        this.mListener = mListener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new MonitorItem();
    }

    private class MonitorItem implements AdapterItem<MonitorItemVo> {

        private ImageView itemStatusImageview;
        private TextView itemStatusTv;
        private TextView abnormalMonitorTv;
        private TextView normalMonitorTv;
        private TextView monitorItemTv;
        private LinearLayout itemLayout;

        private MonitorItemVo mData;
        private int mPosition;

        @Override
        public int getLayoutResId() {
            return R.layout.item_monitor;
        }

        @Override
        public void bindViews(View root) {

            itemStatusImageview = (ImageView) root.findViewById(R.id.item_status_imageview);
            itemStatusTv = (TextView) root.findViewById(R.id.item_status_tv);
            abnormalMonitorTv = (TextView) root.findViewById(R.id.abnormal_monitor_tv);
            normalMonitorTv = (TextView) root.findViewById(R.id.normal_monitor_tv);
            monitorItemTv = (TextView) root.findViewById(R.id.monitor_item_tv);
            itemLayout = (LinearLayout) root.findViewById(R.id.item_layout);

        }

        @Override
        public void setViews() {
            itemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(mData, mPosition);
                }
            });
        }

        @Override
        public void handleData(MonitorItemVo monitorItemVo, int position) {

            mData = monitorItemVo;
            mPosition = position;

            if("1".equals(monitorItemVo.getSituationCode())) {
                itemStatusImageview.setBackgroundResource(R.mipmap.icon_monitor_waring_event);
                itemStatusTv.setText("预警");
            } else {
                itemStatusImageview.setBackgroundResource(R.mipmap.icon_monitor_normal_event);
                itemStatusTv.setText("正常");
            }

            monitorItemTv.setText(monitorItemVo.getName());
            abnormalMonitorTv.setText(monitorItemVo.getErrorCount()+"");
            normalMonitorTv.setText(monitorItemVo.getNormalCount()+"");

        }
    }
}
